#!/bin/bash
set -e;
echo "***input backup_dir: "
echo "default path: /opt/os_backup"
read -t 10 os_backup_dir


if [[ -z ${os_backup_dir} ]];then
   os_backup_dir=/opt/os_backup
fi
if [[ -d ${os_backup_dir} ]];then
  echo "os_backup_dir: ${os_backup_dir}"
  sleep 5;
else
  echo "os_backup_dir: ${os_backup_dir}"
  echo "os_backup_dir is not exists!"
  exit 127
fi

echo "*** start to backup......"

backup_time=`date +%Y_%m%d_%H%M%S`

sudo apt install dc3dd -y;
chmod +x pishrink;
sudo cp pishrink /usr/local/bin/;

echo "start backup os......"

sudo rm -f ${os_backup_dir}/Raspberry_Pi_OS_${backup_time}.img


sudo dc3dd if=/dev/mmcblk0 of=${os_backup_dir}/Raspberry_Pi_OS_${backup_time}.img


echo "start pishrink img......"

sudo pishrink -z ${os_backup_dir}/Raspberry_Pi_OS_${backup_time}.img


